package com.chinapost.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.chinapost.dao.BaseDaoI;
import com.chinapost.po.Tdept;
import com.chinapost.po.Twd;
import com.chinapost.service.WdServiceI;
import com.chinapost.vo.DataGrid;
import com.chinapost.vo.Wd;

@Service("wdService")
public class WdServiceImpl implements WdServiceI {
	
	@Autowired
	private BaseDaoI<Twd> wdDao;
	@Autowired
	private BaseDaoI<Tdept> deptDao;


	@Override
	public void add(Wd wd) {
		Twd t = new Twd();
		BeanUtils.copyProperties(wd, t);
		Tdept dept = deptDao.get(Tdept.class, wd.getDeptid());
		t.setTdept(dept);
		wdDao.save(t);
	}

	@Override
	public void edit(Wd wd) {
		Twd t = wdDao.get(Twd.class, wd.getCjgh());
		BeanUtils.copyProperties(wd, t);
		t.setTdept(deptDao.get(Tdept.class, wd.getDeptid()));
	}

	@Override
	public void delete(Wd wd) {
		String cids = "";
		if (wd.getIds() != null) {
			String ids = wd.getIds();
			for (String cid : ids.split(",")) {
				cids += "'" + cid + "'" + ",";
			}
			cids = cids.substring(0, cids.length() - 1);
			String hql = "delete Twd t where t.cjgh in (" + cids + ")";
			wdDao.executeHql(hql);
		}

	}

	@Override
	public DataGrid datagrid(Wd wd) {
		DataGrid dg = new DataGrid();
		dg.setRows(listRows(find(wd)));
		dg.setTotal(total(wd));
		return dg;
	}

	private Long total(Wd wd) {
		String hql = "select count(*) from Twd t where 1=1";
		Map<String, Object> params = new HashMap<String, Object>();
		hql = addWhere(wd, hql, params);
		return wdDao.count(hql, params);
	}

	private List<Twd> find(Wd wd) {
		String hql = "from Twd t where 1=1";
		Map<String, Object> params = new HashMap<String, Object>();
		hql = addWhere(wd, hql, params);
		if (wd.getOrder() != null && wd.getSort() != null) {
			hql += " order by " + wd.getSort() + " " + wd.getOrder();
		}
		return wdDao.find(hql, params, wd.getPage(), wd.getRows());
	}

	private List<Wd> listRows(List<Twd> lt) {
		List<Wd> rows = new ArrayList<Wd>();
		if (lt != null && lt.size() > 0) {
			for (Twd t : lt) {
				Wd sub = new Wd();
				BeanUtils.copyProperties(t, sub);
				sub.setDeptid(t.getTdept().getCid());
				sub.setDeptname(t.getTdept().getCname());
				rows.add(sub);
			}
		}
		return rows;
	}

	private String addWhere(Wd wd, String hql, Map<String, Object> params) {
		if (wd != null && wd.getCjgh() != null && !wd.getCjgh().trim().equals("")) {
			hql += " and t.cjgh = :id ";
			params.put("id", wd.getCjgh());
		}
		if (wd != null && wd.getCname() != null && !wd.getCname().trim().equals("")) {
			hql += " and t.cname like :wdname ";
			params.put("wdname", "%%" + wd.getCname() + "%%");
		}
		if (wd != null && wd.getDeptIds() != null && wd.getDeptIds().size() > 0) {
			hql += " and t.tdept.cid in(:deptids)";
			params.put("deptids", wd.getDeptIds());
		}
		return hql;
	}


	@Override
	public List<String> getWdIds(String deptid) {
		List<String> wdIds = new ArrayList<String>();
		String hql = "from Twd t where t.tdept.cid =:deptid";
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("deptid", deptid);
		List<Twd> l = wdDao.find(hql, params);
		if (l != null && l.size() > 0) {
			for (Twd t : l) {
				wdIds.add(t.getCjgh());
			}
		}
		return wdIds;
	}

}

